package com.scott.java8concurrency.before5;


/**
 * 可完成的线程
 *
 * @author Scott
 */
public class CompletableRunnableMain {

    public static void main(String[] args) throws InterruptedException {
        CompletableRunnable completableRunnable = new CompletableRunnable();


        Thread thread = new Thread(completableRunnable, "sub线程");
        thread.start();


        //等待线程执行结束，此时tread的线程 就和主线程编程了串行操作了，而不是并行操作了
        thread.join();

        System.out.printf("[thread:%s] staring\n", Thread.currentThread().getName());
        System.out.printf("completableRunnable is completed: %s",completableRunnable.isCompleted());
    }

    private static class CompletableRunnable implements Runnable {


        /**
         * 外部可见
         */
        private  volatile   boolean completed = false;

        @Override
        public void run() {
            System.out.printf("[thread:%s] 执行\n", Thread.currentThread().getName());

            completed = true;
        }

        public boolean isCompleted() {
            return completed;
        }
    }
}
